package com.java.teacherexer.exer3;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    private static int PORT = 9527;

    public static void main(String[] args) {
        System.out.println("服务器启动，开始监听...");
        //启动服务
        AppServer server = new AppServer();
        server.start();
        //运行5分钟后关闭服务
        try {
            Thread.sleep(1000 * 60 * 5);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        server.setRunFlag(false);
    }

    public static class AppServer extends Thread {
        private boolean runFlag = true;
        //建立主机服务套接字
        private ServerSocket server;
        //建立通讯信息的套接字Socket对象
        private Socket sc;
        //接收信息的输入流
        private InputStream ins;
        private ObjectInputStream objIn;
        //发送信息的输出流
        private OutputStream outs;
        private ObjectOutputStream objOut;

        @Override
        public void run() {
            //建立主机，对某个端口监听
            try {
                server = new ServerSocket(PORT);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.out.println("ERROR: 端口被占用");
                return;
            }
            //循环监听
            while (runFlag) {
                try {
                    //阻塞监听
                    sc = server.accept();
                    //解析套接字对象，获取信息
                    //对方的IP
                    String ip = sc.getInetAddress().toString();
                    //对方的端口
                    int port = sc.getPort();
                    //得到自己的ip
                    String myIp = sc.getLocalAddress().toString();
                    //得到自己的端口
                    int myPort = sc.getLocalPort();
                    //获取信息所在的输入流
                    ins = sc.getInputStream();
                    //将输入流封装为对象的输入流
                    objIn = new ObjectInputStream(ins);
                    //读出对象
                    String msg = (String) objIn.readObject();

                    System.out.println("接收到：" + ip + " : " + port + "\n" +
                            "自己：" + myIp + " : " + myPort + "\n" +
                            "消息：" + msg);
                    //给客户端一个回执信息
                    outs = sc.getOutputStream();
                    objOut = new ObjectOutputStream(outs);
                    objOut.writeObject("我收到了");
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } finally {
                    if (outs != null) {
                        try {
                            outs.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    if (ins != null) {
                        try {
                            ins.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    if (sc != null) {
                        try {
                            sc.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }

            try {
            } finally {
                //关闭ServerSocket
                if (server != null) {
                    try {
                        server.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                System.out.println("------服务停止。");
            }

        }

        public boolean isRunFlag() {
            return runFlag;
        }

        public void setRunFlag(boolean runFlag) {
            this.runFlag = runFlag;
        }

    }

}
